package com.xoa.service.equipment.impl;

import com.alibaba.fastjson.JSONArray;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.equipment.*;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.department.Department;
import com.xoa.model.equipment.*;
import com.xoa.model.users.Users;
import com.xoa.service.equipment.EquipmentTransferService;
import com.xoa.util.LimsJson;
import com.xoa.util.UserUtil;
import com.xoa.util.common.StringUtils;
 import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



@Service
public class EquipmentTransferServiceImpl implements EquipmentTransferService {

    //@Autowired
    //private LimsBaseService limsBaseService;

    @Autowired
    private LimsEquipOwnerlogMapper limsEquipOwnerlogMapper;

    @Autowired
    private LimsEquipOwnerlistMapper limsEquipOwnerlistMapper;

    @Autowired
    private LimsDeptLocationMapper limsDeptLocationMapper;
    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private EquipTypeMapper equipTypeMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private EquipCapyMapper equipCapyMapper;

   // @Autowired
    //private LimsDeptExpMapper limsDeptExpMapper;

  /*  @Autowired
    private LimsMaterToolsServiceImpl limsMaterToolsService;*/


    @Override
    public LimsJson selectBypid(Integer pid) {
        LimsJson limsJson = new LimsJson();
        EquipType equipType = equipTypeMapper.selectByPid(pid);
        limsJson.setObject(equipType);
        limsJson.setCode(0);
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   15:17 2019/10/10 0010
    * 方法介绍:   获取当前登录人所在的结算组织，和用户名称
    * 参数说明:   No such property: code for class: Script1
    * @return     [request]
    */
    @Override
    public LimsJson deptExpp(HttpServletRequest request) {
        LimsJson limsJson = new LimsJson();
        Users users = UserUtil.getUser();//得到当前登录人
        List<Department> limsDeptExps = departmentMapper.getDepartmentByUid(users.getUid());
        //List<LimsDeptExp> limsDeptExps = limsBaseService.baseGetDeptExpsByUserId(users.getUserId());//根据userId查询所在的结算组织
        Users user=new Users();//创建新的用户实体用来前端回显
        user.setUserName(users.getUserName());//用户名
        user.setUserId(users.getUserId());//用户id
        if(limsDeptExps.size()>0){
            limsJson.setCode(0);
            limsJson.setData(limsDeptExps);
            limsJson.setObject(user);
            limsJson.setMsg("查询成功");
        }else {
            limsJson.setMsg("查询失败");
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   16:22 2019/10/10 0010
    * 方法介绍:   新增一条设备转移申请
    * 参数说明:   No such property: code for class: Script1
    * @return     [ownerLog, ownerList]
    */
    @Override
    public LimsJson addTransAppr(LimsEquipOwnerlog limsEquipOwnerlog, String ownerList, Integer logId) {
        LimsJson limsJson = new LimsJson();
        try {
            int result = 0;
            if(limsEquipOwnerlog.getApplication()!=null) {
                limsEquipOwnerlog.setApprovalStatus("0");
                limsEquipOwnerlog.setReceiveStatus("0");
                result = limsEquipOwnerlogMapper.insertSelective(limsEquipOwnerlog);
                if (result > 0) {  //添加成功返回logID
                    if (ownerList != null) { //如果设备类型不为空
                        List<LimsEquipOwnerlist> limsEquipOwnerlists= JSONArray.parseArray(ownerList,LimsEquipOwnerlist.class);
                        if (limsEquipOwnerlists.size() > 0) {
                            for (LimsEquipOwnerlist limsEquipOwnerlist : limsEquipOwnerlists) {
                                limsEquipOwnerlist.setOwnerlogId(limsEquipOwnerlog.getOwnerlogId());
                                limsEquipOwnerlist.setReceiveStatus("0");

                                limsEquipOwnerlist.setOriginalPositionId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId())); //存储原部门id
                                Department fiexdColunm = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId()));
                                if(fiexdColunm!=null){
                                    limsEquipOwnerlist.setOriginalPositionName(fiexdColunm.getDeptName());
                                }

                                limsEquipOwnerlist.setNewPositionId(Integer.valueOf(limsEquipOwnerlog.getNewDeptId()));//存储新部门id
                                Department fiexdColunms = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId()));
                                if(fiexdColunms!=null){
                                    limsEquipOwnerlist.setNewPositionName(fiexdColunms.getDeptName());
                                }

                                int i = limsEquipOwnerlistMapper.insertSelective(limsEquipOwnerlist);
                            }
                        }
                    }
                }
            } else if (ownerList != null && logId != null) { //如果不是新增的申请，而是添加设备信息，则走此步骤
                     List<LimsEquipOwnerlist> limsEquipOwnerlists= JSONArray.parseArray(ownerList,LimsEquipOwnerlist.class);
                    if (limsEquipOwnerlists.size() > 0) {
                        for (LimsEquipOwnerlist limsEquipOwnerlist : limsEquipOwnerlists) { ;
                            limsEquipOwnerlist.setOwnerlogId(logId);
                            limsEquipOwnerlist.setReceiveStatus("0");

                            limsEquipOwnerlist.setOriginalPositionId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId()));//存储原部门id
                            Department fiexdColunm = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId()));//根据部门id查询部门
                            if(fiexdColunm!=null){
                                limsEquipOwnerlist.setOriginalPositionName(fiexdColunm.getDeptName());
                            }

                            limsEquipOwnerlist.setNewPositionId(Integer.valueOf(limsEquipOwnerlog.getNewDeptId()));//存储新部门id
                            Department fiexdColunms = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId()));//根据部门id查询部门
                            if(fiexdColunms!=null){
                                limsEquipOwnerlist.setNewPositionName(fiexdColunms.getDeptName());
                            }

                             result = limsEquipOwnerlistMapper.insertSelective(limsEquipOwnerlist);
                        }
                    }
                }
                if(result>0){
                    limsJson.setCode(0);
                    limsJson.setMsg("新增成功");
                }else {
                    limsJson.setMsg("新增失败");
                }

        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   17:18 2019/10/10 0010
    * 方法介绍:   查询所有设备信息根据deptId
    * 参数说明:   No such property: code for class: Script1
    * @return     [request]
    */
    @Override
    public LimsJson findequipBydept(Integer equipTypeId,Integer page,Integer limit,Integer deptId) {
        LimsJson limsJson = new LimsJson();
        Users users = UserUtil.getUser();
        Map map = new HashMap();
        Map map1= new HashMap();
        List<Equipment> equipment = new ArrayList<>();
        PageParams pageParams = new PageParams();
        try{
            map.put("deptId",deptId);
            List<EquipType> equipTypes = equipTypeMapper.selectChildEquipType(equipTypeId);//根据设备类型Id查询是否存在子集
            String typeIds="";
            if(equipTypes.size()>0){   //如果存在获取到所有子集的
                for(int i = 0 ; i<equipTypes.size();i++){
                    typeIds+=equipTypes.get(i).getId()+",";
                 }
                String[] ids=typeIds.split(",");
                map.put("typeIds",ids);
                map1.put("typeIds",ids);
            }else{//如果不存在子集则是最底层的设备类型，直接根据ID查询设备
                map.put("equipTypeId",equipTypeId);
                map1.put("equipTypeId",equipTypeId);
            }
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            map.put("pageParams",pageParams);
            equipment = equipmentMapper.selectAllEquipment(map);
            Iterator<Equipment> iterator = equipment.iterator();
            while(iterator.hasNext()){
                String next = iterator.next().getEquipStatuasId();
                if("EQUIP_SCRAPPED".equals(next)||"EQUIP_LEND".equals(next)){
                    iterator.remove();
                }
            }
                for (Equipment e:equipment){
                if(e.getEquipStatuasId()!=null&&!e.getEquipStatuasId().equals("")){
                        EquipCapy equipCapy = equipCapyMapper.selectCommonCodeByTypeNo(e.getEquipStatuasId(),null);
                        if (equipCapy!=null&&!equipCapy.equals("")){
                            e.setEquipStatuasName(equipCapy.getCodeTypeName());
                        }
                }
                if(e.getPhyQuantiId()!=null&&!e.getPhyQuantiId().equals("")){
                    EquipCapy equipCapy = equipCapyMapper.selectCommonCodeByTypeNo(e.getPhyQuantiId(),null);
                    if(equipCapy!=null&&!equipCapy.equals("")){
                        e.setPhyQuantiId(equipCapy.getCodeTypeName());
                    }
                }
                if(e.getCouplingId()!=null&&!e.getCouplingId().equals("")){
                    EquipCapy equipCapy = equipCapyMapper.selectCommonCodeByTypeNo(e.getCouplingId(),null);
                    if (equipCapy!=null&&!equipCapy.equals("")){
                        e.setCouplingId(equipCapy.getCodeTypeName());
                    }
                }
                if(e.getDirectionId()!=null&&!e.getDirectionId().equals("")){
                    EquipCapy equipCapy = equipCapyMapper.selectCommonCodeByTypeNo(e.getDirectionId(),null);
                    if (equipCapy!=null&&!equipCapy.equals("")){
                        e.setDirectionId(equipCapy.getCodeTypeName());
                    }
                }
                if(e.getPropertyId()!=null&&!e.getPropertyId().equals("")){
                    EquipCapy equipCapy = equipCapyMapper.selectCommonCodeByTypeNo(e.getPropertyId(),"PROPERTY_OWNER");
                    if (equipCapy!=null&&!equipCapy.equals("")){
                        e.setPropertyId(equipCapy.getCodeTypeName());
                    }
                }
                if(e.getSourceAssets()!=null&&!e.getSourceAssets().equals("")){
                    EquipCapy equipCapy = equipCapyMapper.selectCommonCodeByTypeNo(e.getSourceAssets(),null);
                    if (equipCapy!=null&&!equipCapy.equals("")){
                        e.setSourceAssets(equipCapy.getCodeTypeName());
                    }
                }
                if (e.getStandMateYn()!=null&&!e.getStandMateYn().equals("")){
                    if(e.getStandMateYn().equals("0")){
                        e.setStandMateYn("否");
                    }else {
                        e.setStandMateYn("是");
                    }
                }
                if(e.getPositionId()!=null&&!e.getPositionId().equals("")){
                    LimsDeptLocation limsDeptLocation = limsDeptLocationMapper.selectByPrimaryKey(e.getPositionId());
                    if(limsDeptLocation!=null){
                        e.setPositionName(limsDeptLocation.getLabel());
                    }
                }
            }

            if(equipment.size()>0){
                List<Equipment> list=this.getPageList(page,limit,equipment);
                limsJson.setCount(equipment.size());
                limsJson.setMsg("查询成功");
                limsJson.setCode(0);
                limsJson.setData(equipment);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   9:19 2019/10/11 0011
    * 方法介绍:   检查所选设备是否存在已经在申请
    * 参数说明:   No such property: code for class: Script1
    * @return     [request, equipId]
    */

    @Override
    public LimsJson checkChooseEquip(HttpServletRequest request, String  equipIds) {
        LimsJson limsJson = new LimsJson();
        /*Users users = UserUtil.getUser();
        Map map = new HashMap();
        List  list = new ArrayList();
        List  list2 = new ArrayList();
        int i=0;
        int j=0;
        try{
            String[] equipIdsArr=equipIds.split(",");
            map.put("equipIds",equipIdsArr);
            List<LimsEquipOwnerlog> limsEquipOwnerlogs = limsEquipOwnerlogMapper.checkChooseEquip(map);
            if(limsEquipOwnerlogs.size()>0){
                for(LimsEquipOwnerlog limsEquipOwnerlog : limsEquipOwnerlogs){
                    if(limsEquipOwnerlog.getApplication().equals(users.getUserId())){
                    if(limsEquipOwnerlog.getApprovalStatus().equals("1")){
                        list.add(limsEquipOwnerlog);
                        }else if(limsEquipOwnerlog.getApprovalStatus().equals("0")){
                        list.add(limsEquipOwnerlog);
                    }else if(limsEquipOwnerlog.getApprovalStatus().equals("2")){
                        list.add(limsEquipOwnerlog);
                    }
                    }
                }if(list.size()>0){
                    limsJson.setData(list);
                    limsJson.setCode(0);
                    limsJson.setMsg("你已经申请转移该设备");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }*/
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   13:44 2019/10/11 0011
    * 方法介绍:   根据当前人查询申请记录
    * 参数说明:   No such property: code for class: Script1
    * @return     [page, limit, approvalStatus, receiveStatus]
    */
    @Override
    public LimsJson selectOwnerLog(Integer page, Integer limit, String approvalStatus,String column,String inptext) {
        LimsJson limsJson = new LimsJson();
        Users users =UserUtil.getUser();
        Map map =  new HashMap();
        Map map1= new HashMap();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        try{
            map.put("approvalStatus",approvalStatus);
            map.put("application",users.getUserId());
            if(column!=null && inptext!=null){
                Map map2 = new HashMap();
                if( column.equals("APPLICATION") || column.equals("APPROVER") || column.equals("RECEIVE_USER")){
                    map2.put("userName",inptext);
                    List<Users> usersList1 = usersMapper.selByUserNameAndDeptId(map2);
                    String uids="";
                    for(Users users2:usersList1){
                        uids+=users2.getUserId()+",";
                    }
                    String key=lineToHump(column);
                    map1.put("column",column);
                    map1.put("smcolums",key);
                    map1.put("columsValue",uids.split(","));
                    map.put("column",column);
                    map.put("smcolums",key);
                    map.put("columsValue",uids.split(","));
                }  else if( column.equals("ORIGINAL_DEPT_ID") || column.equals("NEW_DEPT_ID")){
                    List<Department> departments = departmentMapper.selByLikeDeptName(inptext);
                    String deptIdsArry="";
                    for(Department department:departments){
                        deptIdsArry+=department.getDeptId()+",";
                    }
                    String key=lineToHump(column);
                    map1.put("column",column);
                    map1.put("smcolums",key);
                    map1.put("columsValue",deptIdsArry.split(","));
                    map.put("column",column);
                    map.put("smcolums",key);
                    map.put("columsValue",deptIdsArry.split(","));
                }else{
                    map1.put("column",column);
                    map1.put("inptext",inptext);
                    map.put("column",column);
                    map.put("inptext",inptext);
                }
            }
            List<LimsEquipOwnerlog> limsEquipOwnerlogs = limsEquipOwnerlogMapper.selectOwnerLog(map);
            for(LimsEquipOwnerlog limsEquipOwnerlog:limsEquipOwnerlogs){
                if(limsEquipOwnerlog.getNewDeptId()!=null&&!limsEquipOwnerlog.getNewDeptId().equals("")){//根据Id查询字段赋值字段名
                    Department newdept = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getNewDeptId()));
                    limsEquipOwnerlog.setNewDeptName(newdept.getDeptName());
                }if(limsEquipOwnerlog.getOriginalDeptId()!=null && !limsEquipOwnerlog.getOriginalDeptId().equals("")){
                    Department olddept = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId()));
                    limsEquipOwnerlog.setOldDeptName(olddept.getDeptName());
                }if(limsEquipOwnerlog.getNewCustodian()!=null && !limsEquipOwnerlog.getNewCustodian().equals("")){
                    Users newCustodian = usersMapper.findUsersByuserId(limsEquipOwnerlog.getNewCustodian());
                    limsEquipOwnerlog.setNewCustodianName(newCustodian.getUserName());
                }
//                if(limsEquipOwnerlog.getOriginalCustodian()!=null && !limsEquipOwnerlog.getOriginalCustodian().equals("")){
//                    Users oldCustodian = usersMapper.findUsersByuserId(limsEquipOwnerlog.getOriginalCustodian());
//                    limsEquipOwnerlog.setOldCustodianName(oldCustodian.getUserName());
//                }
                if(limsEquipOwnerlog.getApplication()!=null && !limsEquipOwnerlog.getApplication().equals("")){
                    Users application = usersMapper.findUsersByuserId(limsEquipOwnerlog.getApplication());
                    limsEquipOwnerlog.setApplicationName(application.getUserName());
                }if(limsEquipOwnerlog.getApprover()!=null && !limsEquipOwnerlog.getApprover().equals("")){
                    Users approval = usersMapper.findUsersByuserId(limsEquipOwnerlog.getApprover());
                    limsEquipOwnerlog.setApprovalName(approval.getUserName());
                }
            }
            if(limsEquipOwnerlogs.size()>0){
                map1.put("approvalStatus",approvalStatus);
                map1.put("application",users.getUserId());
                int count = limsEquipOwnerlogMapper.contInfo(map1);
                limsJson.setMsg("查询成功");
                limsJson.setCode(0);
                limsJson.setCount(count);
                limsJson.setData(limsEquipOwnerlogs);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }


    /**
     * 创建作者:   庞君
     * 创建日期:   13:51 2019/10/11 0011
     * 方法介绍:   根据ownerId查询设备信息
     * 参数说明:   No such property: code for class: Script1
     * @return
     */
    @Override
    public LimsJson findBylogId(Integer page, Integer limit, Integer ownerlogId) {
        LimsJson limsJson = new LimsJson();
        Map map = new HashMap();
        Map map1=new HashMap();
        PageParams pageParams = new PageParams();
        pageParams.setPageSize(limit);
        pageParams.setPage(page);
        pageParams.setUseFlag(true);
        map.put("page",pageParams);
        try{
            map.put("ownerlogId",ownerlogId);
            List<LimsEquipOwnerlist> bylogId = limsEquipOwnerlistMapper.findBylogId(map);
            if(bylogId.size()>0){
                for(LimsEquipOwnerlist limsEquipOwnerlist :bylogId){
                    map1.put("equipId",limsEquipOwnerlist.getEquipId());
                    EquipCapy equipCapy = equipCapyMapper.selectCommonCodeByTypeNo(limsEquipOwnerlist.getEquipStatuasId(),null);
                    if (equipCapy!=null&&!equipCapy.equals("")){
                        limsEquipOwnerlist.setEquipStatuasId(equipCapy.getCodeTypeName());
                    }
                }
//                for(LimsEquipOwnerlist limsEquipOwnerlist : bylogId){
//                    EquipCapy equipCapy = equipCapyMapper.selectCommonCodeByTypeNo(limsEquipOwnerlist.getEquipStatuasCode());
//                    limsEquipOwnerlist.setEquipStatuasCode(equipCapy.getCodeTypeName());
//                }
                limsJson.setCount(pageParams.getTotal());
                limsJson.setCode(0);
                limsJson.setData(bylogId);
                limsJson.setMsg("查询成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   14:03 2019/10/11 0011
    * 方法介绍:   删除设备信息
    * 参数说明:   No such property: code for class: Script1
    * @return     [ids]
    */
    @Override
    public LimsJson delByKey(String ids) {
        LimsJson limsJson = new LimsJson();
        Map map = new HashMap();
        try{
            String[] idsArray=ids.split(",");
            map.put("ids",idsArray);
            int i = limsEquipOwnerlistMapper.delByKey(map);
            if(i>0){
                limsJson.setCode(0);
                limsJson.setMsg("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   14:39 2019/10/11 0011
    * 方法介绍:   删除申请信息
    * 参数说明:   No such property: code for class: Script1
    * @return     [logIds]
    */
    @Override
    public LimsJson delBylogId(String logIds) {
        LimsJson limsJson = new LimsJson();
       Map map = new HashMap();
       try{
           String[] logIdsArray = logIds.split(",");
           map.put("logIds",logIdsArray);
           List<LimsEquipOwnerlist> logIdLists = limsEquipOwnerlistMapper.findBylogId(map);//删除前先判断是否在对应数据
           if(logIdLists.size()>0){//存在先删除子表数据
               int i = limsEquipOwnerlistMapper.delByKey(map);
               if(i>0){
                   int result = limsEquipOwnerlogMapper.delBylogId(map);
                   if(result>0){
                       limsJson.setCode(0);
                       limsJson.setMsg("删除成功");
                   }
               }
           }else {//不存在直接删除主表数据
               int result = limsEquipOwnerlogMapper.delBylogId(map);
               if(result>0){
                   limsJson.setCode(0);
                   limsJson.setMsg("删除成功");
               }
           }
       }catch (Exception e){
           e.printStackTrace();
       }
        return limsJson;
    }


    /**
    * 创建作者:   庞君
    * 创建日期:   15:18 2019/10/11 0011
    * 方法介绍:   提交申请,审批（批准不批准）
    * 参数说明:   No such property: code for class: Script1
    * @return     [approvalStatus, logIds]
    */
    @Override
    public LimsJson updStatus(String approvalStatus,String recUser,String logIds,String  deptIds) {
        LimsJson limsJson = new LimsJson();
        Users users = UserUtil.getUser();
        Map map = new HashMap();
        Map map1 = new HashMap();
        int i=0;
        int res=0;
        try{
            String[] logIdsArray=logIds.split(",");
            map.put("logIds",logIdsArray);
            map.put("approvalStatus",approvalStatus);
            String equipIds="";
            List<LimsEquipOwnerlist> bylogId = limsEquipOwnerlistMapper.findBylogId(map); //根据所提交的logId查询所有的equipId
            for(int j=0;j<bylogId.size();j++){
                equipIds+=bylogId.get(j).getEquipId()+",";
            }
            if(approvalStatus.equals("1")){  //提交时判断
                Map map2= new HashMap();
                map2.put("equipIds",equipIds.split(","));
                map2.put("approvalStatus",approvalStatus);
                List<LimsEquipOwnerlog> limsEquipOwnerlogs = limsEquipOwnerlogMapper.checkChooseEquip(map2);//根据equipId和提交状态查询申请表中是否存在已经被别人提交
                if(limsEquipOwnerlogs.size()>0){
                    limsJson.setCode(0);
                    limsJson.setData(limsEquipOwnerlogs);
                    limsJson.setMsg("设备已提交申请");
                    return limsJson;
                }else if(limsEquipOwnerlogs.size()<0){
                    List<Equipment> infoByEquipId = equipmentMapper.getInfoByEquipId(map2);
                    List list = new ArrayList();
                    String[] deptId=deptIds.split(",");
                    for(Equipment e:infoByEquipId){
                        for(int m=0;m<deptId.length;m++){
                            if(deptId[i].equals(e.getDeptId())){
                                list.add(e);
                            }
                        }

                    }
                    if(list.size()>0){
                        limsJson.setCode(0);
                        limsJson.setData(list);
                        limsJson.setMsg("设备已经被转移或当前部门不存在此设备");
                        return limsJson;
                    }

                } else {
                    i = limsEquipOwnerlogMapper.updStatus(map);//提交申请
                }
            } else if(approvalStatus.equals("2")){
                map.put("approver",users.getUserId());
                map.put("approverTime",new Date());
                map.put("receiveUser",recUser);
                i = limsEquipOwnerlogMapper.updStatus(map);
                    if(i>0){
                        Equipment equipment = new Equipment();
                        for(int j=0;j<logIdsArray.length;j++){
                            map1.put("ownerlogId",logIdsArray[j]);
                            LimsEquipOwnerlog limsEquipOwnerlog = limsEquipOwnerlogMapper.selectByPrimaryKey(Integer.valueOf(logIdsArray[j]));
                            List<LimsEquipOwnerlist> lists = limsEquipOwnerlistMapper.findBylogId(map1);
                            for(LimsEquipOwnerlist list: lists){
                                equipment.setDeptId(Integer.valueOf(limsEquipOwnerlog.getNewDeptId()));
                                equipment.setEquipId(list.getEquipId());
                                equipment.setCustodian(limsEquipOwnerlog.getNewCustodian());
                                equipmentMapper.updateByPrimaryKeySelective(equipment);
                            }
                        }

                }
            }else if(approvalStatus.equals("3")){
                map.put("approver",users.getUserId());
                map.put("approverTime",new Date());
                i = limsEquipOwnerlogMapper.updStatus(map);
            }if(i>0){
                limsJson.setCode(0);
                limsJson.setMsg("提交成功");
            }else{
                limsJson.setMsg("提交失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   10:21 2019/10/12 0012
    * 方法介绍:   设备接收
    * 参数说明:   No such property: code for class: Script1
    * @return     [ownerlogId, logIds, ownerlistId, receiveStatus, newPositionId, newPositionName, memo]
    */
    @Override
    public LimsJson updRecStatus( Integer ownerlogId,Integer ownerlistId,String receiveStatus,String newPositionName) {
        LimsJson limsJson = new LimsJson();
        Users users = UserUtil.getUser();
        Map map = new HashMap();
        Map map2= new HashMap();
        Map map3= new HashMap();
        try{
            LimsEquipOwnerlog limsEquipOwnerlog = new LimsEquipOwnerlog();
            //接受前端传过来的接受状态值，和ID
            map.put("receiveStatus",receiveStatus);
            map.put("receiveUser",users.getUserId());
            map.put("receiveTime",new Date());
          //  map.put("newPositionId",newPositionId);
            map.put("newPositionName",newPositionName);
          //  map.put("memo",memo);
            map.put("ownerlogId",ownerlogId);
            if(ownerlistId!= null){
                map.put("ownerlistId",ownerlistId);
            }
            int i = limsEquipOwnerlistMapper.updRecStatus(map);
            if(i>0){//接收成功后修改equipment表中的设备信息
                limsEquipOwnerlog.setReceiveUser(users.getUserId());
                limsEquipOwnerlog.setOwnerlogId(ownerlogId);
               limsEquipOwnerlogMapper.updateByPrimaryKeySelective(limsEquipOwnerlog);
                Equipment equipment = new Equipment();
                List<LimsEquipOwnerlist> lists = limsEquipOwnerlistMapper.findBylogId(map);//根据ownerlogId和接受状态查询设备信息设备ID
                for(LimsEquipOwnerlist limsEquipOwnerlist1:lists){
                    equipment.setEquipId(limsEquipOwnerlist1.getEquipId());
                    equipment.setPositionId(limsEquipOwnerlist1.getNewPositionId());
                   equipmentMapper.update(equipment);//修改设备表中的部门ID和存放位置
                        }
                map2.put("receiveStatus","0");
                map2.put("ownerlogId",ownerlogId);
                List<LimsEquipOwnerlist> list1 = limsEquipOwnerlistMapper.findBylogId(map2);//查询对应的ownerLogId下是否存在未接收设备
                map3.put("ownerlogId",ownerlogId);
                if(list1.size()>0){
                    map3.put("receiveStatus","1");//如果存在则给ownerLog表中的接受状态为部分接受
                }else {
                    map3.put("receiveStatus","2");//不存在则为全部接受
                }
                int i1 = limsEquipOwnerlogMapper.updStatus(map3);//修改申请表中接受状态
                limsJson.setCode(0);
                limsJson.setMsg("接收成功");
            }else {
                limsJson.setMsg("接收失败");
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   11:35 2019/10/12 0012
    * 方法介绍:   设备转移管理
    * 参数说明:   No such property: code for class: Script1
    * @return     [request, page, limit, approvalStatus]
    */
    @Override
    public LimsJson adminastorList(HttpServletRequest request, Integer page, Integer limit,String approvalStatus,String column,String inptext) {
        LimsJson limsJson = new LimsJson();
        Map map = new HashMap();
        Users users = UserUtil.getUser();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setUseFlag(true);
        pageParams.setPageSize(limit);

        Integer deptId = users.getDeptId();
        String deptIdOther = users.getDeptIdOther();
        if (!StringUtils.checkNull(deptIdOther)) {
            deptIdOther = deptId + "," + deptIdOther;
        } else {
            deptIdOther = deptId + ",";
        }
        String[] deptIds = deptIdOther.split(",");

        List<Department> departments = departmentMapper.selDeptByIds(deptIds);

        String a="";//得到结算组织的deptId
        if(departments.size()>0){
            for(Department limsDeptExp: departments){
                a+=limsDeptExp.getDeptId()+",";
            }
        }
        String[] str = a.split(",");
        List list = new ArrayList();
        for(int i= 0;i<str.length;i++){//数组去重
            if(!list.contains(str[i])){
                list.add(str[i]);
            }
        }
        map.put("deptIds",str);
        List<Users> usersList = usersMapper.selectUserArrayByDeptId(map);//获取当前所在结算部门的下的所有人员
        List list2= new ArrayList();
        for(Users u:usersList){
            list2.add(u.getUserId());
        }
        Map map1 = new HashMap();
        map1.put("userIds",list2);
        map1.put("approvalStatus",approvalStatus);
        map1.put("page",pageParams);
        if(column!=null && inptext!=null){
            Map map2 = new HashMap();
            if( column.equals("APPLICATION") || column.equals("APPROVER") || column.equals("RECEIVE_USER")){
                map2.put("userName",inptext);
                List<Users> usersList1 = usersMapper.selByUserNameAndDeptId(map2);
                String uids="";
                for(Users users2:usersList1){
                    uids+=users2.getUserId()+",";
                }
                String key=lineToHump(column);
                map1.put("column",column);
                map1.put("smcolums",key);
                map1.put("columsValue",uids.split(","));
                map.put("column",column);
                map.put("smcolums",key);
                map.put("columsValue",uids.split(","));
            }  else if( column.equals("ORIGINAL_DEPT_ID") || column.equals("NEW_DEPT_ID")){
                List<Department> d = departmentMapper.selByLikeDeptName(inptext);
                String deptIdsArry="";
                for(Department department:d){
                    deptIdsArry+=department.getDeptId()+",";
                }
                String key=lineToHump(column);
                map1.put("column",column);
                map1.put("smcolums",key);
                map1.put("columsValue",deptIdsArry.split(","));
                map.put("column",column);
                map.put("smcolums",key);
                map.put("columsValue",deptIdsArry.split(","));
            }else{
                map1.put("column",column);
                map1.put("inptext",inptext);
                map.put("column",column);
                map.put("inptext",inptext);
            }
        }
        List<LimsEquipOwnerlog> limsEquipOwnerlogs = limsEquipOwnerlogMapper.AdminastorList(map1);//获取数据
        if(limsEquipOwnerlogs.size()>0){
            for(LimsEquipOwnerlog limsEquipOwnerlog:limsEquipOwnerlogs){
                if(limsEquipOwnerlog.getNewDeptId()!=null&&!limsEquipOwnerlog.getNewDeptId().equals("")){//根据Id查询字段赋值字段名
                    Department newdept = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getNewDeptId()));
                    limsEquipOwnerlog.setNewDeptName(newdept.getDeptName());
                }if(limsEquipOwnerlog.getOriginalDeptId()!=null && !limsEquipOwnerlog.getOriginalDeptId().equals("")){
                    Department olddept = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId()));
                    limsEquipOwnerlog.setOldDeptName(olddept.getDeptName());
                }
                if(limsEquipOwnerlog.getNewCustodian()!=null && !limsEquipOwnerlog.getNewCustodian().equals("")){
                    Users newCustodian = usersMapper.findUsersByuserId(limsEquipOwnerlog.getNewCustodian());
                    limsEquipOwnerlog.setNewCustodianName(newCustodian.getUserName());
                }
                if(limsEquipOwnerlog.getApplication()!=null && !limsEquipOwnerlog.getApplication().equals("")){
                    Users application = usersMapper.findUsersByuserId(limsEquipOwnerlog.getApplication());
                    limsEquipOwnerlog.setApplicationName(application.getUserName());
                }if(limsEquipOwnerlog.getApprover()!=null && !limsEquipOwnerlog.getApprover().equals("")){
                    Users approval = usersMapper.findUsersByuserId(limsEquipOwnerlog.getApprover());
                    limsEquipOwnerlog.setApprovalName(approval.getUserName());
                }
            }
        }
        map.put("userIds",list2);
        map.put("approvalStatus",approvalStatus);
        int count = limsEquipOwnerlogMapper.contInfo(map);//获取总数
        if(limsEquipOwnerlogs.size()>0){
            limsJson.setCode(0);
            limsJson.setData(limsEquipOwnerlogs);
            limsJson.setCount(count);
            limsJson.setMsg("查询成功");
        }else{
            limsJson.setMsg("暂无数据");
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   13:37 2019/10/12 0012
    * 方法介绍:   设备接收位置下拉(父类）
    * 参数说明:   No such property: code for class: Script1
    * @return     [request, ownerlogId]
    */
    @Override
    public List<DataTree> reciviList(HttpServletRequest request, Integer ownerlogId) {
        DataTree dataTrees = new DataTree();
        List list = new ArrayList();
        list.add(dataTrees);
        LimsEquipOwnerlog limsEquipOwnerlog = limsEquipOwnerlogMapper.selectByPrimaryKey(ownerlogId);
        Department department = new Department();
        department.setDeptId(Integer.valueOf(limsEquipOwnerlog.getNewDeptId()));


        List<LimsDeptLocation> parentLocation = limsDeptLocationMapper.findParentLocation(department.getDeptId());
        for (LimsDeptLocation limsDeptLocation: parentLocation) {
            DataTree dataTree=new DataTree();

            //循环查找子级
            //查找父级下的子级或同级
            List<LimsDeptLocation> childLocation = limsDeptLocationMapper.findChildLocation(limsDeptLocation.getId());
            if (childLocation.size()>0){
                //有子级
                dataTree.setChildren(getChildren(limsDeptLocation.getId())); //添加子级
                dataTree.setIsLeaf(false);  //有子级
            }else{
                //无子级
                dataTree.setIsLeaf(true);  //无子级
            }
            dataTree.setId(limsDeptLocation.getId());
            dataTree.setName(limsDeptLocation.getLabel());
            list.add(dataTree); //添加到父级LIST
        }
        return list;
    }

    public List<DataTree> getChildren(Integer id) {
        List<DataTree> list = new ArrayList<>();

        List<LimsDeptLocation> childLocation = limsDeptLocationMapper.findChildLocation(id);
        if (childLocation.size()>0){
            for (LimsDeptLocation limsDeptLocation2: childLocation) {
                DataTree dataTree=new DataTree();

                List<LimsDeptLocation> childLocation2 = limsDeptLocationMapper.findChildLocation(limsDeptLocation2.getId());
                if (childLocation2.size()>0){
                    //有子级
                    dataTree.setIsLeaf(false);  //有子级
                    dataTree.setChildren(getChildren(limsDeptLocation2.getId()));
                }else{
                    //无子级
                    dataTree.setIsLeaf(true);  //有子级
                }
                dataTree.setId(limsDeptLocation2.getId());
                dataTree.setName(limsDeptLocation2.getLabel());
                list.add(dataTree);
            }
        }
        return list;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   13:48 2019/10/12 0012
    * 方法介绍:   设备下拉子类
    * 参数说明:   No such property: code for class: Script1
    * @return     [positionId]
    */
    @Override
    public LimsJson reciviChilList(Integer id) {
        LimsJson limsJson = new LimsJson();
       /* List<LimsDeptLocation> childLocation = limsDeptLocationMapper.findChildLocation(id);
        if(childLocation.size()>0){
            limsJson.setCode(0);
            limsJson.setData(childLocation);
            limsJson.setMsg("查询成功");
        }else{
            limsJson.setMsg("查询失败");
        }*/
        return limsJson;
    }
    
    /**
    * 创建作者:   庞君
    * 创建日期:   16:26 2019/10/15 0015
    * 方法介绍:   编辑数据回显
    * 参数说明:   No such property: code for class: Script1
    * @return
    */
    @Override
    public LimsJson editApprovalInfo(Integer ownerlogId) {
        LimsJson limsJson = new LimsJson();
        Map map = new HashMap();
        LimsEquipOwnerlog limsEquipOwnerlog = limsEquipOwnerlogMapper.selectByPrimaryKey(ownerlogId);
        if(limsEquipOwnerlog.getNewDeptId()!=null&&!limsEquipOwnerlog.getNewDeptId().equals("")){//根据Id查询字段赋值字段名
            Department newdept = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getNewDeptId()));
            limsEquipOwnerlog.setNewDeptName(newdept.getDeptName());
        }if(limsEquipOwnerlog.getOriginalDeptId()!=null && !limsEquipOwnerlog.getOriginalDeptId().equals("")){
            Department olddept = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId()));
            limsEquipOwnerlog.setOldDeptName(olddept.getDeptName());
        }
        if(limsEquipOwnerlog.getNewCustodian()!=null && !limsEquipOwnerlog.getNewCustodian().equals("")){
            Users newCustodian = usersMapper.findUsersByuserId(limsEquipOwnerlog.getNewCustodian());
            limsEquipOwnerlog.setNewCustodianName(newCustodian.getUserName());
        }
        if(limsEquipOwnerlog.getApplication()!=null && !limsEquipOwnerlog.getApplication().equals("")){
            Users application = usersMapper.findUsersByuserId(limsEquipOwnerlog.getApplication());
            limsEquipOwnerlog.setApplicationName(application.getUserName());
        }if(limsEquipOwnerlog.getApprover()!=null && !limsEquipOwnerlog.getApprover().equals("")){
            Users approval = usersMapper.findUsersByuserId(limsEquipOwnerlog.getApprover());
            limsEquipOwnerlog.setApprovalName(approval.getUserName());
        }
        map.put("ownerlogId",ownerlogId);
        limsJson.setObject(limsEquipOwnerlog);
        limsJson.setCode(0);
        limsJson.setMsg("查询成功");
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   16:37 2019/10/15 0015
    * 方法介绍:   修改申请信息
    * 参数说明:   No such property: code for class: Script1
    * @return     [limsEquipOwnerlog]
    */
    @Override
    public LimsJson editApproval(LimsEquipOwnerlog limsEquipOwnerlog) {
        LimsJson limsJson = new LimsJson();
        if (limsEquipOwnerlog!=null){
            int i = limsEquipOwnerlogMapper.updateByPrimaryKeySelective(limsEquipOwnerlog);
            if(i>0){
                limsJson.setCode(0);
                limsJson.setMsg("编辑成功");
            }
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   13:01 2019/10/16 0016
    * 方法介绍:   设备转移接收列表
    * 参数说明:   No such property: code for class: Script1
    * @return     [request, map]
    */
    @Override
    public LimsJson selectRceInfo(HttpServletRequest request,Integer page,Integer limit, Map map,String column,String inptext) {
        LimsJson limsJson = new LimsJson();
        Users users =UserUtil.getUser();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        pageParams.setUseFlag(true);
        map.put("page",pageParams);
        map.put("userId", users.getUserId());
        if(column!=null && inptext!=null){
            Map map2 = new HashMap();
            if( column.equals("APPLICATION") || column.equals("APPROVER") || column.equals("RECEIVE_USER")){
                map2.put("userName",inptext);
                List<Users> usersList1 = usersMapper.selByUserNameAndDeptId(map2);
                String uids="";
                for(Users users2:usersList1){
                    uids+=users2.getUserId()+",";
                }
                String key=lineToHump(column);
                map.put("column",column);
                map.put("smcolums",key);
                map.put("columsValue",uids.split(","));
            }  else if( column.equals("ORIGINAL_DEPT_ID") || column.equals("NEW_DEPT_ID")){
                List<Department> departments = departmentMapper.selByLikeDeptName(inptext);
                String deptIdsArry="";
                for(Department department:departments){
                    deptIdsArry+=department.getDeptId()+",";
                }
                String key=lineToHump(column);
                map.put("column",column);
                map.put("smcolums",key);
                map.put("columsValue",deptIdsArry.split(","));
            }else{
                map.put("column",column);
                map.put("inptext",inptext);
            }
        }
        List<LimsEquipOwnerlog> limsEquipOwnerlogs = limsEquipOwnerlogMapper.selectRceInfo(map);
        if(limsEquipOwnerlogs.size()>0){
            for(LimsEquipOwnerlog limsEquipOwnerlog :limsEquipOwnerlogs){
                if(limsEquipOwnerlog.getNewDeptId()!=null&&!limsEquipOwnerlog.getNewDeptId().equals("")){//根据Id查询字段赋值字段名
                    Department newdept = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getNewDeptId()));
                    limsEquipOwnerlog.setNewDeptName(newdept.getDeptName());
                }if(limsEquipOwnerlog.getOriginalDeptId()!=null && !limsEquipOwnerlog.getOriginalDeptId().equals("")){
                    Department olddept = departmentMapper.selectByDeptId(Integer.valueOf(limsEquipOwnerlog.getOriginalDeptId()));
                    limsEquipOwnerlog.setOldDeptName(olddept.getDeptName());
                }if(limsEquipOwnerlog.getNewCustodian()!=null && !limsEquipOwnerlog.getNewCustodian().equals("")){
                    Users newCustodian = usersMapper.findUsersByuserId(limsEquipOwnerlog.getNewCustodian());
                    limsEquipOwnerlog.setNewCustodianName(newCustodian.getUserName());
                }
                    if(limsEquipOwnerlog.getApplication()!=null && !limsEquipOwnerlog.getApplication().equals("")){
                    Users application = usersMapper.findUsersByuserId(limsEquipOwnerlog.getApplication());
                    limsEquipOwnerlog.setApplicationName(application.getUserName());
                }if(limsEquipOwnerlog.getApprover()!=null && !limsEquipOwnerlog.getApprover().equals("")){
                    Users approval = usersMapper.findUsersByuserId(limsEquipOwnerlog.getApprover());
                    limsEquipOwnerlog.setApprovalName(approval.getUserName());
                }
            }
            limsJson.setCount(pageParams.getTotal());
          limsJson.setCode(0);
          limsJson.setMsg("查询成功");
          limsJson.setData(limsEquipOwnerlogs);
        }else{
            limsJson.setMsg("暂无数据");
        }
        return limsJson;
    }

    /**
    * 创建作者:   庞君
    * 创建日期:   15:59 2019/10/17 0017
    * 方法介绍:   转移新部门查询
    * 参数说明:   No such property: code for class: Script1
    * @return     [request]
    */
    @Override
    public LimsJson findRecDept(HttpServletRequest request) {
        LimsJson limsJson = new LimsJson();
        Users users = UserUtil.getUser(request);
        List<Department> allDepartment = departmentMapper.getAllDepartment();
        if(allDepartment!=null&&allDepartment.size()>0){
            limsJson.setData(allDepartment);
            limsJson.setCode(0);
            limsJson.setMsg("查询成功");
        }else{
            limsJson.setMsg("请添加实验室之后再进行操作");
            limsJson.setCode(1);
        }
        return limsJson;
    }

    @Override
    public LimsJson transferData(HttpServletRequest request,Integer limit,Integer page, Integer equipId, java.sql.Date transferDate, String originalDeptId,
                                 String newDeptId, String approvalStatus, String receiveStatus, String workflowNo,String inptext,String column) {
        LimsJson limsJson = new LimsJson();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPageSize(limit);
        pageParams.setPage(page);
        Map map = new HashMap();
        map.put("equipId",equipId);
        map.put("transferDate",transferDate);
        map.put("originalDeptId",originalDeptId);
        map.put("newDeptId",newDeptId);
        map.put("approvalStatus",approvalStatus);
        map.put("receiveStatus",receiveStatus);
        map.put("workflowNo",workflowNo);
        if(column!=null && inptext!=null){
            Map map1 = new HashMap();
            if( column.equals("APPLICATION")){
                    map1.put("userName",inptext);
                List<Users> users = usersMapper.selByUserNameAndDeptId(map1);
                String uids="";
                for(Users users2:users){
                    uids+=users2.getUserId()+",";
                }
                String key=lineToHump(column);
                map.put("column",column);
                map.put("smcolums",key);
                map.put("columsValue",uids.split(","));
            }  else if( column.equals("ORIGINAL_DEPT_ID") || column.equals("NEW_DEPT_ID")){
                List<Department> departments = departmentMapper.selByLikeDeptName(inptext);
                String deptIds="";
                for(Department department:departments){
                    deptIds+=department.getDeptId()+",";
                }
                String key=lineToHump(column);
                map.put("column",column);
                map.put("smcolums",key);
                map.put("columsValue",deptIds.split(","));
            }else{
                map.put("column",column);
                map.put("inptext",inptext);
            }
        }
        List<LimsEquipOwnerlog> limsEquipOwnerlogs = limsEquipOwnerlogMapper.transferData(map);
        if(limsEquipOwnerlogs.size()>0){
            for(LimsEquipOwnerlog leol:limsEquipOwnerlogs){
                if(leol.getApplication()!=null){
                    Users usersByuserId = usersMapper.findUsersByuserId(leol.getApplication());
                    leol.setApplication(usersByuserId.getUserName());
                }
                if(leol.getNewDeptId()!=null){
                    Department department = departmentMapper.selectByDeptId(Integer.valueOf(leol.getNewDeptId()));
                    leol.setNewDeptId(department.getDeptName());
                }
                if(leol.getOriginalDeptId()!=null){
                    Department department = departmentMapper.selectByDeptId(Integer.valueOf(leol.getOriginalDeptId()));
                    leol.setOriginalDeptId(department.getDeptName());
                }
            }
            int i = limsEquipOwnerlogMapper.transferDataCount(map);
            limsJson.setMsg("查询成功");
            limsJson.setData(limsEquipOwnerlogs);
            limsJson.setCode(0);
            limsJson.setCount(i);
        }

        return limsJson;
    }
    /**
     * 创建作者:   庞君
     * 创建日期:   10:12 2019/12/19
     * 方法介绍:   下划线转驼峰
     * 参数说明:   No such property: code for class: Script1
     * @return
     */
    private static Pattern linePattern = Pattern.compile("_(\\w)");
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    //业务层分页
    public List getPageList(Integer page, Integer limit, List list) {
        List list1 = new ArrayList();
        if (page == 1 && list.size() <= limit) {
            return list;
        } else if (page == 1 && list.size() > limit) {
            for (int i = 0; i < limit; i++) {
                list1.add(list.get(i));
            }
            return list1;

        } else if (page > 1) {
            if (list.size() / limit >= page) {
                for (int i = 0; i < limit; i++) {
                    list1.add(list.get(limit * (page - 1) + i));

                }
                return list1;
            } else {
                for (int i = 0; i < list.size() % 10; i++) {
                    list1.add(list.get(limit*(page-1) + i));
                }
            }

            return list1;

        } else {
            return null;
        }
    }
}
